/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.action.account;

import java.util.List;

import org.zhiqim.account.ZacSessionUser;
import org.zhiqim.account.dbo.user.UserInfo;
import org.zhiqim.account.dbo.sys.ZacPayment;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.core.Action;
import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.orm.ZTable;
import org.zhiqim.orm.dbo.Selector;

/**
 * 用户充值
 *
 * @version v1.0.0 @author zzy 2018-4-16 新建与整理
 */
public class ChargeAction implements Action
{

    @Override
    public void execute(HttpRequest request) throws Exception
    {
        ZacSessionUser sessionUser = request.getSessionUser(ZacSessionUser.class);
        
        UserInfo user = Global.get(ZTable.class).item(UserInfo.class, new Selector("userBalance, userPayment", "userId", sessionUser.getUserId()));
        List<ZacPayment> paymentList = Global.get(ZTable.class).list(ZacPayment.class, new Selector("paymentStatus", 0).addOrderbyAsc("paymentSeq"));
        if (paymentList == null)
        {
            request.returnHistory("暂无支付方式，请联系管理员添加");
            return;
        }
        
        //找到第一个支付方式
        ZacPayment firstPayment = null;
        String userPayment = user.getUserPayment();
        if (Validates.isNotEmpty(userPayment))
        {
            for (ZacPayment payment : paymentList)
            {
                if (userPayment.equals(payment.getPaymentCode()))
                {
                    firstPayment = payment;
                    break;
                }
            }
        }
        
        request.setAttribute("firstPayment", (firstPayment == null)?paymentList.get(0):firstPayment);
        request.setAttribute("userBalance", user.getUserBalance());
        request.setAttribute("paymentList", paymentList);
    }

}
